//
//  Register.m
//  KYRR
//
//  Created by kyjun on 15/11/2.
//
//

#import "Register.h"
#import "AppDelegate.h"

@interface Register ()

@property(nonatomic,strong) UIView* headerView;
@property(nonatomic,strong) UIButton* btnTop;
@property(nonatomic,strong) UITextField* txtUserName;
@property(nonatomic,strong) UITextField* txtPassword;
@property(nonatomic,strong) UITextField* txtEmail;
@property(nonatomic,strong) UIButton* btnRegister;
@property(nonatomic,strong) UIButton* btnHasAccount;
@property(nonatomic,strong) UIImageView* line;

@end

@implementation Register

-(instancetype)init{
    self = [super init];
    if(self){
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutUI];
    [self layoutConstraints];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.title= @"用户注册";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)layoutUI{
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 320.f)];
    self.btnTop = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnTop.backgroundColor = theme_navigation_color;
    [self.btnTop setTitle:@"欢迎加入易米大家庭" forState:UIControlStateNormal];
    [self.btnTop setTitleColor:theme_default_color forState:UIControlStateNormal];
    [self.headerView addSubview:self.btnTop];
    
    UILabel* label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 5, 45);
    
    self.txtUserName = [[UITextField alloc]init];
    self.txtUserName.placeholder = @"请填写用户名 (2-15位)";
    self.txtUserName.backgroundColor = theme_default_color;
    self.txtUserName.borderStyle=UITextBorderStyleNone;
    self.txtUserName.layer.borderColor =theme_line_color.CGColor;
    self.txtUserName.layer.borderWidth = 1.0f;
    self.txtUserName.layer.masksToBounds = YES;
    self.txtUserName.layer.cornerRadius = 5.f;
    self.txtUserName.leftView = label;
    self.txtUserName.leftViewMode =UITextFieldViewModeAlways;
    [self.headerView addSubview:self.txtUserName];
    
    label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 5, 45);
    UIButton* btn  =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 21, 13);
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
    [btn setImage:[UIImage imageNamed:@"icon-secure"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(switchTouch:) forControlEvents:UIControlEventTouchUpInside];
    self.txtPassword = [[UITextField alloc]init];
    self.txtPassword.backgroundColor = theme_default_color;
    self.txtPassword.secureTextEntry = YES;
    self.txtPassword.placeholder = @"请填写密码 (至少6位)";
    self.txtPassword.borderStyle=UITextBorderStyleNone;
    self.txtPassword.layer.borderColor =theme_line_color.CGColor;
    
    self.txtPassword.layer.borderWidth = 1.0f;
    self.txtPassword.layer.masksToBounds = YES;
    self.txtPassword.layer.cornerRadius = 5.f;
    self.txtPassword.leftView = label;
    self.txtPassword.leftViewMode =UITextFieldViewModeAlways;
    self.txtPassword.rightView = btn;
    self.txtPassword.rightViewMode = UITextFieldViewModeAlways;
    [self.headerView addSubview:self.txtPassword];
    
    
    label = [[UILabel alloc]init];
    label.frame = CGRectMake(0, 0, 5, 45);
    
    self.txtEmail = [[UITextField alloc]init];
    self.txtEmail.placeholder = @"请填写手机/邮箱";
    self.txtEmail.backgroundColor = theme_default_color;
    self.txtEmail.borderStyle=UITextBorderStyleNone;
    self.txtEmail.layer.borderColor =theme_line_color.CGColor;
    self.txtEmail.layer.borderWidth = 1.0f;
    self.txtEmail.layer.masksToBounds = YES;
    self.txtEmail.layer.cornerRadius = 5.f;
    self.txtEmail.leftView = label;
    self.txtEmail.leftViewMode =UITextFieldViewModeAlways;
    [self.headerView addSubview:self.txtEmail];
    
    self.btnRegister = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnRegister.backgroundColor = theme_navigation_color;
    [self.btnRegister setTitleColor:theme_default_color forState:UIControlStateNormal];
    [self.btnRegister setTitle:@"注册" forState:UIControlStateNormal];
    [self.btnRegister addTarget:self action:@selector(registerTouch:) forControlEvents:UIControlEventTouchUpInside];
    self.btnRegister.layer.masksToBounds = YES;
    self.btnRegister.layer.cornerRadius = 5.f;
    self.btnRegister.layer.borderColor = theme_line_color.CGColor;
    [self.headerView addSubview:self.btnRegister];
    
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"已有账号"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSForegroundColorAttributeName value:theme_title_color range:strRange];
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    
    self.btnHasAccount = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnHasAccount setTitleColor:theme_Fourm_color forState:UIControlStateNormal];
    [self.btnHasAccount setAttributedTitle:str forState:UIControlStateNormal];
    [self.btnHasAccount addTarget:self action:@selector(backTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.btnHasAccount];
    
    self.line = [[UIImageView alloc]init];
    self.line.backgroundColor = theme_line_color;
    [self.headerView addSubview:self.line];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = self.headerView;
    
}

-(void)layoutConstraints{
    self.btnTop.translatesAutoresizingMaskIntoConstraints = NO;
    self.txtUserName.translatesAutoresizingMaskIntoConstraints = NO;
    self.txtPassword.translatesAutoresizingMaskIntoConstraints = NO;
    self.txtEmail.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnRegister.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnHasAccount.translatesAutoresizingMaskIntoConstraints = NO;
    self.line.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    [self.btnTop addConstraint:[NSLayoutConstraint constraintWithItem:self.btnTop attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnTop addConstraint:[NSLayoutConstraint constraintWithItem:self.btnTop attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnTop attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnTop attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.txtUserName addConstraint:[NSLayoutConstraint constraintWithItem:self.txtUserName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.txtUserName addConstraint:[NSLayoutConstraint constraintWithItem:self.txtUserName attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-20]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtUserName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btnTop attribute:NSLayoutAttributeBottom multiplier:1.0 constant:15.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtUserName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    
    [self.txtPassword addConstraint:[NSLayoutConstraint constraintWithItem:self.txtPassword attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.txtPassword addConstraint:[NSLayoutConstraint constraintWithItem:self.txtPassword attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-20]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtPassword attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.txtUserName attribute:NSLayoutAttributeBottom multiplier:1.0 constant:15.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtPassword attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.txtEmail addConstraint:[NSLayoutConstraint constraintWithItem:self.txtEmail attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.txtEmail addConstraint:[NSLayoutConstraint constraintWithItem:self.txtEmail attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-20]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtEmail attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.txtPassword attribute:NSLayoutAttributeBottom multiplier:1.0 constant:15.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtEmail attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    
    [self.btnRegister addConstraint:[NSLayoutConstraint constraintWithItem:self.btnRegister attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnRegister addConstraint:[NSLayoutConstraint constraintWithItem:self.btnRegister attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-20]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnRegister attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.txtEmail attribute:NSLayoutAttributeBottom multiplier:1.0 constant:15.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnRegister attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.btnHasAccount addConstraint:[NSLayoutConstraint constraintWithItem:self.btnHasAccount attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.btnHasAccount addConstraint:[NSLayoutConstraint constraintWithItem:self.btnHasAccount attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnHasAccount attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btnRegister attribute:NSLayoutAttributeBottom multiplier:1.0 constant:15.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnHasAccount attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10.f]];
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
}

#pragma mark =====================================================  SEL
-(IBAction)switchTouch:(id)sender{
    UIButton* btn = (UIButton*)sender;
    btn.selected = !btn.selected;
    self.txtPassword.secureTextEntry = !btn.selected;
}

-(IBAction)registerTouch:(id)sender{
    [self checkNetWorkState:^(AFNetworkReachabilityStatus netWorkStatus) {
        if(netWorkStatus!=AFNetworkReachabilityStatusNotReachable){
            if([self checkForm]){
                self.HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
                [self.view.window addSubview:self.HUD];
                self.HUD.delegate = self;
                self.HUD.minSize = CGSizeMake(135.f, 135.f);
                [self.HUD setLabelFont:[UIFont systemFontOfSize:14.f]];
                self.HUD.labelText = @"正在登录!";
                [self.HUD show:YES];
                
                NSDictionary* arg = @{@"ince":@"reg_user",@"uname":self.txtUserName.text,@"passwd":self.txtPassword.text,@"bind":self.txtEmail.text};
               
                NetRepositories* repositories = [[NetRepositories alloc]init];
                [repositories login:arg complete:^(NSInteger react, id obj, NSString *message) {
                    if(react == 1){
                        MMember* entity = (MMember*)obj;
                        entity.isLogin = YES;
                        Boolean flag= [NSKeyedArchiver archiveRootObject:entity toFile:[WMHelper archiverPath]];
                        if(flag){
                            [[NSNotificationCenter defaultCenter]postNotificationName:NotificationLoginSuccess object:nil];
                        }else{
                            //NSLog(@"%@",@"归档失败!");
                        }
                        [self hidHUD:@"登录成功" success:YES];
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }else if(react == 400){
                        [self hidHUD:message];
                    }else{
                        [self hidHUD:message];
                    }
                }];

            }
        }
    }];
}

-(IBAction)backTouch:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark =====================================================  私有方法
-(BOOL)checkForm{
    if(self.txtUserName.text.length<2 || self.txtUserName.text.length>15){
        [self alertHUD:@"请正确填写用户名!"];
        return NO;
    }else if(self.txtPassword.text.length<6){
        [self alertHUD:@"密码至少6位!"];
        return NO;
    }else if(!([WMHelper isValidateEmail:self.txtEmail.text] ||[WMHelper isValidateMobile:self.txtEmail.text])){
        [self alertHUD:@"请正确填写手机或邮箱！"];
        return NO;
    }
    return YES;
}

@end
